#include "Sesssion.hpp"
#include "Http.hpp"
#include "TcpServer.hpp"
#include <memory>

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " localport" << std::endl;
}

HttpResponse Login(HttpRequest& req)
{
    // 登录的时候，你需要什么？？username and passwd
    // 
    HttpResponse resp;
    resp.SetCode(302);
    resp.SetCookie("Set-Cookie", "k=1");
    // resp.SetCookie("Set-Cookie", "session_token=abcde12345; Max-Age=3600; Path=/; Secure; HttpOnly");
    // resp.SetCookie("Set-Cookie", "theme=dark; Max-Age=3600; Path=/; Secure");
    resp.SetHeader("Location", "/");

    LOG(LogLevel::DEBUG)<< "-----Login: " << req.Body();

    return resp;
}
HttpResponse Register(HttpRequest& req)
{
    LOG(LogLevel::DEBUG)<< "-----Register" << req.Body();
}

HttpResponse Search(HttpRequest& req)
{
    LOG(LogLevel::DEBUG)<< "-----Search"<< req.Body();
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    EnableConsoleLogStrategy();
    // HTTP 协议
    std::unique_ptr<Http> http = std::make_unique<Http>();
    http->Register("/login", Login);
    http->Register("/register", Register);
    http->Register("/search", Search);

    // 网络服务
    uint16_t serverport = std::stoi(argv[1]);
    std::unique_ptr<TcpServer> tsock = std::make_unique<TcpServer>(serverport,
        [&http](std::string &reqstr)->std::string{
            return http->HandlerRequst(reqstr);
    });
    tsock->Run();

    return 0;
}
